package controller

import (
	"errors"
	"fmt"
	"github.com/e421083458/gin_scaffold/dao"
	"github.com/e421083458/gin_scaffold/dto"
	"github.com/e421083458/gin_scaffold/middleware"
	"github.com/e421083458/golang_common/lib"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

/**
队列：
基本操作是入队(Enqueue)，在表的末端插入一个元素
出队(Dequeue)，删除(或返回)在表头的元素
*/

type MotoController struct {
}

type Item interface {}
// 队列结构体
type Queue struct {
	Items []Item
}

type IQueue interface {
	New() Queue
	Enqueue(t Item)
	Dequeue(t Item)
	IsEmpty() bool
	Size() int
}
func MotoRegister(router *gin.RouterGroup) {
	moto := MotoController{}
	//实现一个队列
	router.GET("/queue", moto.Queue) //队列

	router.GET("/motolist", moto.Motolist) //产品列表
	router.GET("/motobyid", moto.Motobyid) //通过id查产品
	router.GET("/flashbuy", moto.Flashbuy) //抢购

}

// IsEmpty 队列是否为空
func (q *Queue) IsEmpty() bool  {
	return len(q.Items) == 0
}

// Size 队列长度
func (q *Queue) Size() int  {
	return len(q.Items)
}

// New 新建
func (q *Queue)New() *Queue  {
	q.Items = []Item{}
	return q
}

// Enqueue 入队
func (q *Queue) Enqueue(data Item)  {
	q.Items = append(q.Items, data)
}

// Dequeue 出队
func (q *Queue) Dequeue() *Item {
	// 由于先进先出，0为队首
	item := q.Items[0]
	q.Items = q.Items[1:len(q.Items)]
	return &item
}

var q Queue
func initQueue() *Queue  {
	if q.Items == nil{
		q = Queue{}
		q.New()
	}
	return &q
}
// Queue 将马上开奖的投入队列
func (moto *MotoController) Queue(c *gin.Context) {
	initQueue()
	//查询当前要出售不指定的车机

}

func (moto *MotoController) Motobyid(c *gin.Context) {
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 2000, err)
		return
	}

	addInput := &dto.FindMoto{}
	if err := addInput.BindingValidParams(c); err != nil {
		middleware.ResponseError(c, 2001, err)
		return
	}


	lists,errr:= (&dao.TaskConfig{}).Findbyid(c, tx,addInput.Id)

	if errr != nil {
		middleware.ResponseError(c, 2001, errr)
		return
	}

	fmt.Println(lists)
	middleware.ResponseSuccess(c, lists)
	return
}

func (moto *MotoController) Motolist(c *gin.Context) {
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 2000, err)
		return
	}

	lists,errr:= (&dao.TaskConfig{}).FindShoudSell(c, tx,1)

	if errr != nil {
		middleware.ResponseError(c, 2001, errr)
		return
	}
	
	middleware.ResponseSuccess(c, lists)
	return
}
func (moto *MotoController) Flashbuy(c *gin.Context) {
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 2000, err)
		return
	}
	params := &dto.FlashBuy{}
	if err := params.BindingValidParams(c); err != nil {
		middleware.ResponseError(c, 2001, err)
		return
	}
	pigId:=params.Id
	uid:=params.Uid
	//车机信息
	lists,errr:= (&dao.TaskConfig{}).Findbyid(c, tx,pigId)
	if errr != nil {
		middleware.ResponseError(c, 2001, errr)
		return
	}

	haveBuy:=(&dao.Yuyue{}).FindQiang(c,tx,pigId,uid)
	//避免重复抢购
	if haveBuy ==1 {
		middleware.ResponseError(c, 2001,errors.New("今日已抢购"))
		return
	}
	//验证时间
	nowTime:=time.Now().Unix()
	now:=time.Now()
	dayStart :=strconv.Itoa(now.Year())+"-"+strconv.Itoa(int(now.Month()))+"-"+ strconv.Itoa(now.Day())+" "+lists.StartTime+":00"
	start :=dao.TimetoS(dayStart)
	dayEnd:=strconv.Itoa(now.Year())+"-"+strconv.Itoa(int(now.Month()))+"-"+ strconv.Itoa(now.Day())+" "+lists.EndTime+":00"
	end :=dao.TimetoS(dayEnd)
	//if nowTime<start||nowTime>end{
	//	fmt.Println("时间不对")
	//	middleware.ResponseError(c, 2001,errors.New("不是抢购时间"))
	//	return
	//}

	//查看用户状态
	user,err:=(&dao.Users{}).FindUser(c,tx,  strconv.Itoa(uid))
	if user.Status==0{
			middleware.ResponseError(c, 2001,errors.New("账户冻结"))
			return
	}
	//实名验证
	userIden:=(&dao.Iden{}).FindIden(c,tx,uid)
	if userIden==0 {
		middleware.ResponseError(c, 2001,errors.New("实名认证未通过"))
		return
	}

	//金豆余额
	haveYueyue:=(&dao.Yuyue{}).FindQiang(c,tx,pigId,uid)
	if haveYueyue ==0 {
		if lists.QiangPoints>user.PayPoints{
			middleware.ResponseError(c, 2001,errors.New("金豆不足"))
			return
		}

	}
	fmt.Println(user)
	fmt.Println(nowTime)
	fmt.Println(start)
	fmt.Println(nowTime)
	fmt.Println(end)

	fmt.Println(lists)

}

